#-*- coding: utf-8 -*-
__author__ = 'P.Trebert'
"""

usage:

python u06a3.1.py <img>
Esc -> Alle Fenster schließen

"""
import sys

import cv2


class Filter(object):
    """Bietet verschiedene Filterfunktionen auf Bildern"""
    def __init__(self, name):
        self.name = name
        self.img = self.read_image(name)
        #self.img = cv2.resize(self.img,(800, 600))

        # Median-Blur
        self.median_blur_window_name = 'Median-Blur'
        self.median_blur_img = self.img.copy()
        ## Kernel-Size immer Quadratisch a x a
        self.median_blur_ksize_name = 'Kernelgr.'
        self.median_blur_ksize = 3  # [3, 5, 7, ...]
        self.median_blur_ksize_max = 100  # slider Beginnt IMMER bei 0

        # Bilateral-Filter
        self.bilateral_filter_window_name = 'Bilateral-Filter'
        self.bilateral_filter_img = self.img.copy()
        ## Durchmesser, einzubeziehender Pixelnachbarn
        self.bilateral_filter_diameter_name = 'Durchmesser'
        self.bilateral_filter_diameter = 5
        self.bilateral_filter_diameter_max = 21  # slider Beginnt IMMER bei 0
        ## Sigma-Color, wieviel Farben von Nachbarpixeln benutzt werden
        self.bilateral_filter_sigma_color_name = 'Sig.Color'
        self.bilateral_filter_sigma_color = 50
        self.bilateral_filter_sigma_color_max = 250  # slider Beginnt IMMER bei 0
        ## Sigma-Space, wieviel Farben von Nachbarpixeln benutzt werden
        self.bilateral_filter_sigma_space_name = 'Sig.Space'
        self.bilateral_filter_sigma_space = 50
        self.bilateral_filter_sigma_space_max = 250  # slider Beginnt IMMER bei 0
        ## Border-Typ
        self.bilateral_filter_border_type_name = 'Rand'
        self.bilateral_filter_border_type = cv2.BORDER_DEFAULT
        self.bilateral_filter_border_type_max = 5  # slider Beginnt IMMER bei 0

        # Gauss-Filter
        self.gaussian_blur_window_name = 'Gauss-Blur'
        self.gaussian_blur_img = self.img.copy()
        ## Kernel-Groesse
        self.gaussian_blur_kernel_x_name = 'Kernel_X'
        self.gaussian_blur_kernel_y_name = 'Kernel_y'
        self.gaussian_blur_kernel_x = 1  # muss ungerade sein, wenn 0,0 -> aus standardardabweichung berr
        self.gaussian_blur_kernel_y = 1
        self.gaussian_blur_kernel_x_max = 20
        self.gaussian_blur_kernel_y_max = 20
        ## Standardabweichung
        self.gaussian_blur_sigma_x_name = 'Std.Abw._X'
        self.gaussian_blur_sigma_y_name = 'Std.Abw._y'
        self.gaussian_blur_sigma_x = 0
        self.gaussian_blur_sigma_y = 0
        self.gaussian_blur_sigma_x_max = 20
        self.gaussian_blur_sigma_y_max = 20
        ## Border-Typ
        self.gaussian_blur_border_type_name = 'Rand'
        self.gaussian_blur_border_type = cv2.BORDER_DEFAULT
        self.gaussian_blur_border_type_max = 5  # slider Beginnt IMMER bei 0

        # Box-Filter
        self.box_filter_window_name = 'Box-Filter'
        self.box_filter_img = self.img.copy()
        ## Kernel
        self.box_filter_kernel_x_name = 'Kernel_X'
        self.box_filter_kernel_y_name = 'Kernel_y'
        self.box_filter_kernel_x = 1  # nur ungerade sinnvoller
        self.box_filter_kernel_y = 1
        self.box_filter_kernel_x_max = 250  # nur ungerade Zahlen
        self.box_filter_kernel_y_max = 250  # nur ungerade Zahlen
        ## Bildtiefe ddepth
        self.box_filter_ddepth_name = 'Bildtiefe'
        self.box_filter_ddepth = -1  # -1 -> Tiefe des Input-Bildes
        self.box_filter_ddepth_max = 6  # slider Beginnt IMMER bei 0
        ## Ankerpunkte (Mitte des Kernels)
        self.box_filter_anchor_x_name = 'Anker_X'
        self.box_filter_anchor_y_name = 'Anker_Y'
        self.box_filter_anchor_x = -1  # nicht kleiner als 2
        self.box_filter_anchor_y = -1
        self.box_filter_anchor_x_max = 500  # doppelt so viele wie Kernel-Laengen
        self.box_filter_anchor_y_max = 500
        ## Normalisierter Kernel-Flag
        self.box_filter_normalized_flag_name = 'Normalis.'
        self.box_filter_normalized_flag = 1, 1
        self.box_filter_normalized_flag_max = 1  # slider Beginnt IMMER bei 0
        ## Border-Typ
        self.box_filter_border_type_name = 'Rand'
        self.box_filter_border_type = cv2.BORDER_DEFAULT
        self.box_filter_border_type_max = 5  # slider Beginnt IMMER bei 0

    def create_median_blur(self):
        """Erstellt Fenter, Slider und Belegt Werte fur den Medianblur"""
        # check ob 1,3,4 Kanalbild
        if self.median_blur_img.shape[2] not in [1, 3, 4]:
            raise ValueError("Medianblur() unterstuetzt nur 1, 3, 4 Kanalbilder!")

        cv2.namedWindow(self.median_blur_window_name, cv2.CV_WINDOW_AUTOSIZE)
        ## Durchmesser
        cv2.createTrackbar(self.median_blur_ksize_name, self.median_blur_window_name,
                           self.median_blur_ksize, self.median_blur_ksize_max,
                           self.on_median_blur_ksize)

    def create_bilateral_filter(self):
        """Erstellt Fenter, Slider und Belegt Werte fur den Bilateralfilter"""
        if self.bilateral_filter_img.shape[2] not in [1, 3]:
            raise ValueError("BilatralFilter() unterstuetzt nur 1, 3 Kanalbilder!")
        cv2.namedWindow(self.bilateral_filter_window_name, cv2.CV_WINDOW_AUTOSIZE)
        ## Durchmesser
        cv2.createTrackbar(self.bilateral_filter_diameter_name, self.bilateral_filter_window_name,
                           self.bilateral_filter_diameter, self.bilateral_filter_diameter_max,
                           self.on_bilateral_filter_diameter)
        ## Sigma-Color
        cv2.createTrackbar(self.bilateral_filter_sigma_color_name, self.bilateral_filter_window_name,
                           self.bilateral_filter_sigma_color, self.bilateral_filter_sigma_color_max,
                           self.on_bilateral_filter_sigma_color)
        ## Sigma-Space
        cv2.createTrackbar(self.bilateral_filter_sigma_space_name, self.bilateral_filter_window_name,
                           self.bilateral_filter_sigma_space, self.bilateral_filter_sigma_space_max,
                           self.on_bilateral_filter_sigma_space)
        ## Bordertyp
        cv2.createTrackbar(self.bilateral_filter_border_type_name, self.bilateral_filter_window_name,
                           self.bilateral_filter_border_type, self.bilateral_filter_border_type_max,
                           self.on_bilateral_filter_border_type)

    def create_box_filter(self):
        """Erstellt Fenter, Slider und Belegt Werte fur den Boxfilter"""
        cv2.namedWindow(self.box_filter_window_name, cv2.CV_WINDOW_AUTOSIZE)
        ## Kernel
        cv2.createTrackbar(self.box_filter_kernel_x_name, self.box_filter_window_name, self.box_filter_kernel_x,
                           self.box_filter_kernel_x_max, self.on_box_filter_kernel_x)
        cv2.createTrackbar(self.box_filter_kernel_y_name, self.box_filter_window_name, self.box_filter_kernel_y,
                           self.box_filter_kernel_y_max, self.on_box_filter_kernel_y)
        ## Anker
        cv2.createTrackbar(self.box_filter_anchor_x_name, self.box_filter_window_name, self.box_filter_anchor_x,
                           self.box_filter_anchor_x_max, self.on_anchor_x)
        cv2.createTrackbar(self.box_filter_anchor_y_name, self.box_filter_window_name, self.box_filter_anchor_y,
                           self.box_filter_anchor_y_max, self.on_anchor_y)
        ## Bordertyp
        cv2.createTrackbar(self.box_filter_border_type_name, self.box_filter_window_name,
                           self.box_filter_border_type, self.box_filter_border_type_max, self.on_border_type)
        ## Bildtiefe
        cv2.createTrackbar(self.box_filter_ddepth_name, self.box_filter_window_name, self.box_filter_ddepth,
                           self.box_filter_ddepth_max, self.on_ddepth)
        ## Normalsierungsflag
        cv2.createTrackbar(self.box_filter_normalized_flag_name, self.box_filter_window_name,
                           self.box_filter_normalized_flag[0], self.box_filter_normalized_flag_max, self.on_normalized_flag)

    def create_gaussian_blur(self):
        """Erstellt Fenter, Slider und Belegt Werte fur den Gauss-Blur Filter"""
        cv2.namedWindow(self.gaussian_blur_window_name, cv2.CV_WINDOW_AUTOSIZE)
        ## Kernel
        cv2.createTrackbar(self.gaussian_blur_kernel_x_name, self.gaussian_blur_window_name,
                           self.gaussian_blur_kernel_x, self.gaussian_blur_kernel_x_max, self.on_gaussian_blur_kernel_x)
        cv2.createTrackbar(self.gaussian_blur_kernel_y_name, self.gaussian_blur_window_name,
                           self.gaussian_blur_kernel_y, self.gaussian_blur_kernel_y_max, self.on_gaussian_blur_kernel_y)
        ## Standardabweichung Sigma
        cv2.createTrackbar(self.gaussian_blur_sigma_x_name, self.gaussian_blur_window_name,
                           self.gaussian_blur_sigma_x, self.gaussian_blur_sigma_x_max, self.on_gaussian_blur_sigma_x)
        cv2.createTrackbar(self.gaussian_blur_sigma_y_name, self.gaussian_blur_window_name,
                           self.gaussian_blur_sigma_y, self.gaussian_blur_sigma_y_max, self.on_gaussian_blur_sigma_y)
        ## Bordertyp
        cv2.createTrackbar(self.gaussian_blur_border_type_name, self.gaussian_blur_window_name,
                           self.gaussian_blur_border_type, self.gaussian_blur_border_type_max, self.on_gaussian_blur_border_type)

    def show(self):
        """Erstellt alle Fenster, Slider und zeigt alles an, ruft die jeweiligen update() Methoden auf"""
        self.create_median_blur()
        self.create_bilateral_filter()
        self.create_gaussian_blur()
        self.create_box_filter()
        while True:
            self.update_median_blur_filter()
            self.update_bilateral_filter()
            self.update_gaussian_blur()
            self.update_box_filter()
            if cv2.waitKey(27) != -1:  # Esc - Abbruch
                cv2.destroyAllWindows()
                break

    def update_median_blur_filter(self):
        """Zeigt Bild mit medianBlur() in eigenem Fenster an.

        Muss mindestens einmal aufgerufen werden um Fenster anzuzeigen.
        -> gefiltertes Bild

        """
        src = self.median_blur_img
        dst = None
        ksize = self.median_blur_ksize

        img2 = cv2.medianBlur(src=src, ksize=ksize, dst=dst)
        cv2.imshow(self.median_blur_window_name, img2)
        return img2

    def update_bilateral_filter(self):
        """Zeigt Bild mit Bilateral-Filter in eigenem Fenster an.

        Muss mindestens einmal aufgerufen werden um Fenster anzuzeigen.
        -> gefiltertes Bild

        """
        src = self.bilateral_filter_img
        dst = None
        d = self.bilateral_filter_diameter
        sigma_color = self.bilateral_filter_sigma_color
        sigma_space = self.bilateral_filter_sigma_space
        border_type = self.bilateral_filter_border_type

        img2 = cv2.bilateralFilter(src=src, d=d, sigmaColor=sigma_color, sigmaSpace=sigma_space, dst=dst, borderType=border_type)
        cv2.imshow(self.bilateral_filter_window_name, img2)
        return img2

    def update_gaussian_blur(self):
        """Zeigt Bild mit Gauss-Blur-Funktion in eigenem Fenster an.

        Muss mindestens einmal aufgerufen werden um Fenster anzuzeigen.
        -> gefiltertes Bild

        """
        # Parameter fuer den BoxFilter
        src = self.gaussian_blur_img
        ksize = self.gaussian_blur_kernel_x, self.gaussian_blur_kernel_y  # Wenn (0,0) -> bestimmt sigma den Wert
        sigma_x = self.gaussian_blur_sigma_x
        sigma_y = self.gaussian_blur_sigma_y  # Wenn 0 -> selbe wie sigma_x
        dst = self.gaussian_blur_img
        border_type = self.gaussian_blur_border_type

        # gefiltertes Bild erstellen und anzeigen
        cv2.GaussianBlur(src=src, ksize=ksize, sigmaX=sigma_x, dst=dst, sigmaY=sigma_y, borderType=border_type)
        cv2.imshow(self.gaussian_blur_window_name, self.gaussian_blur_img)
        return self.gaussian_blur_img

    def update_box_filter(self,):
        """Zeigt Bild mit BoxFilter-Funktion in eigenem Fenster an.

        Muss mindestens einmal aufgerufen werden um Fenster anzuzeigen.
        -> gefiltertes Bild

        """
        #  Parameter fuer den BoxFilter
        src = self.box_filter_img
        ddepth = self.box_filter_ddepth
        ksize = self.box_filter_kernel_x, self.box_filter_kernel_y  # Objekt selbst fragen, nicht den Slider
        dst = None  # Ausgabebild
        anchor = self.box_filter_anchor_x, self.box_filter_anchor_y
        normalize = self.box_filter_normalized_flag[0]  # (1,1) -> True??
        border_type = self.box_filter_border_type

        # gefiltertes Bild erstellen und anzeigen
        img2 = cv2.boxFilter(src=src, ddepth=ddepth, ksize=ksize, dst=dst, anchor=anchor, normalize=normalize,
                             borderType=border_type)
        cv2.imshow(self.box_filter_window_name, img2)
        return img2

    #  Hilfsfunktionen
    def get_channels_and_dtype(self, img):
        """-> (channels, dtype)"""
        dtype = img.dtype
        channels = img.shape[2]
        print dtype, channels
        return channels, dtype

    def get_border_types(self):
        """Gibt Liste mit cv2 Bordertypen zurueck"""
        border_types = [cv2.BORDER_DEFAULT,
                        cv2.BORDER_REFLECT101,
                        cv2.BORDER_REFLECT,
                        cv2.BORDER_CONSTANT,
                        #cv2.BORDER_ISOLATED, # unsupported Bordertype
                        cv2.BORDER_REFLECT_101,
                        cv2.BORDER_REPLICATE,
                        #cv2.BORDER_TRANSPARENT, # unsupported Bordertype
                        #cv2.BORDER_WRAP # unsupported Bordertype
                        ]
        return border_types

    def get_border_names(self):
        """Gibt Liste mit Namen der cv2 Bordertypen zurueck"""
        border_names = ['cv2.BORDER_DEFAULT',
                        'cv2.BORDER_REFLECT101',
                        'cv2.BORDER_REFLECT',
                        'cv2.BORDER_CONSTANT',
                        #'cv2.BORDER_ISOLATED',  # unsupported Bordertype
                        'cv2.BORDER_REFLECT_101',
                        'cv2.BORDER_REPLICATE',
                        #'cv2.BORDER_TRANSPARENT', # unsupported Bordertype
                        #'cv2.BORDER_WRAP' # unsupported Bordertype
                        ]
        return border_names

    def use_fresh_img_gauss(self):
        """Verwendet das Unveraenderte Bild fuer aktuelle Bildmanipulation und wendet diese an."""
        self.gaussian_blur_img = self.img.copy()
        self.update_gaussian_blur()

    #  Slider Callback-Funktionen

    def on_median_blur_ksize(self, value):
        """Immer quadratischer Kernel"""
        l = range(3, self.median_blur_ksize_max * 2 + 4, 2)
        self.median_blur_ksize = l[value]
        print 'len ', len(l)

    def on_bilateral_filter_diameter(self, value):
        """Empfohlen value==5 - Realtime Apps, value==9 - offline Apps
        wenn <0 proportional zu sigma_space"""
        l = range(-1, 21)
        val = l[value]
        if val < 1 <= self.bilateral_filter_diameter:  # gerade gewechselt von 1 -> 0
                print 'Durchmesser < 0 => Durchmesser ist jetzt proportional zu Sigma_Space!'
        elif self.bilateral_filter_diameter < 1 <= val:
            cv2.setTrackbarPos(self.bilateral_filter_sigma_space_name, self.bilateral_filter_window_name, 0)
            print 'Durchmesser > 0 => Sigma-Space hat keinen Einfluss mehr auf Filtervorgang.'

        self.bilateral_filter_diameter = val
        print 'Bilateral-Filter Durchmesser ', self.bilateral_filter_diameter

    def on_bilateral_filter_sigma_color(self, value):
        """Kleine Werte sind < 10, große Werte > 150. Kann selben Wert wie sigma_Space haben"""
        self.bilateral_filter_sigma_color = value
        print 'Bilateral-Filter Sigma-Color ', self.bilateral_filter_sigma_color

    def on_bilateral_filter_sigma_space(self, value):
        """Kleine Werte sind < 10, große Werte > 150. Kann selben Wert wie sigma_Color haben"""
        self.bilateral_filter_sigma_space = value
        print 'Bilateral-Filter Sigma-Space ', self.bilateral_filter_sigma_space

    def on_bilateral_filter_border_type(self, value):
        self.bilateral_filter_border_type = self.get_border_types()[value]
        print 'Gauss Border-Typ ', self.get_border_names()[value]

    def on_gaussian_blur_border_type(self, value):
        self.gaussian_blur_border_type = self.get_border_types()[value]
        self.use_fresh_img_gauss()
        print 'Gauss Border-Typ ', self.get_border_names()[value]

    def on_gaussian_blur_sigma_x(self, value):
        l = range(self.gaussian_blur_sigma_x_max + 1)
        val = l[value]
        if val == 0:
            if self.gaussian_blur_kernel_x == 0 or self.gaussian_blur_kernel_y == 0:  # darf nicht vorkommen cv2 api
                cv2.setTrackbarPos(self.gaussian_blur_kernel_x_name, self.gaussian_blur_window_name, 1)
                cv2.setTrackbarPos(self.gaussian_blur_kernel_y_name, self.gaussian_blur_window_name, 1)
                print "Gauss: Sigma_X == 0. Kernel Breite und Hoehe duerfen nicht auch 0 sein.\n" \
                      "Kernelgroesse wird auf (1,1) gesetzt."

        self.gaussian_blur_sigma_x = val

        self.use_fresh_img_gauss()
        print 'Gauss Standardabweichung X ', self.gaussian_blur_sigma_x

    def on_gaussian_blur_sigma_y(self, value):
        l = range(self.gaussian_blur_sigma_y_max + 1)
        val = l[value]
        self.gaussian_blur_sigma_y = val
        # wenn 0,0 von kernelgroesse abgeleitete abweichung
        self.use_fresh_img_gauss()
        print 'Gauss Standardabweichung Y ', self.gaussian_blur_sigma_y if val != 0 else '0 -> d.h. (sigma_X) %s' % \
            self.gaussian_blur_sigma_x

    def on_gaussian_blur_kernel_x(self, value):
        l = [0] + [val for val in range(1, self.gaussian_blur_kernel_x_max*2 + 4, 2)]  # nur ungerade Kernelgroessen
        val = l[value]
        self.gaussian_blur_kernel_x = val  # ok
        if self.gaussian_blur_sigma_x == 0:
            print 'Gauss: Kernel und Standardabweichung duerfen nicht beide 0 sein. Setze jetzt Sigma auf (1,1).'
            cv2.setTrackbarPos(self.gaussian_blur_sigma_x_name, self.gaussian_blur_window_name, 1)
            cv2.setTrackbarPos(self.gaussian_blur_sigma_y_name, self.gaussian_blur_window_name, 1)
        self.use_fresh_img_gauss()
        print 'Gauss Kernel X ', self.gaussian_blur_kernel_x

    #ksize.width > 0 && ksize.width % 2 == 1 && ksize.height > 0 && ksize.height % 2 == 1
    def on_gaussian_blur_kernel_y(self, value):
        l = [0] + [val for val in range(1, self.gaussian_blur_kernel_x_max*2 + 4, 2)]  # nur ungerade Kernelgroessen
        val = l[value]
        self.gaussian_blur_kernel_y = val  # ok
        if self.gaussian_blur_sigma_x == 0:
            print 'Gauss: Kernel und Standardabweichung duerfen nicht beide 0 sein. Setze jetzt Sigma auf (1,1).'
            cv2.setTrackbarPos(self.gaussian_blur_sigma_x_name, self.gaussian_blur_window_name, 1)
            cv2.setTrackbarPos(self.gaussian_blur_sigma_y_name, self.gaussian_blur_window_name, 1)
        self.use_fresh_img_gauss()
        print 'Gauss Kernel Y ', self.gaussian_blur_kernel_y

    def on_border_type(self, value):
        self.box_filter_border_type = self.get_border_types()[value]
        print 'Border-Typ ', self.get_border_names()[value]

    def on_normalized_flag(self, value):
        self.box_filter_normalized_flag = value, value
        print 'Normalized ', self.box_filter_normalized_flag[0] == 1

    def on_anchor_x(self, value):
        lis = range(-1, self.box_filter_anchor_x_max)
        val = lis[value]
        # Check ob Werte innerhalb der Kernelgroesse bleiben
        if -1 <= val < self.box_filter_kernel_x:
            self.box_filter_anchor_x = val
        else:
            raise ValueError("Wert Anker_X == %s ist nicht erlaubt. Kernel-Breite == %s -> zu klein." %
                             (val, self.box_filter_kernel_x))
        if val == -1 and self.box_filter_anchor_y is not -1:    # anker y auch auf -1
            cv2.setTrackbarPos(self.box_filter_anchor_y_name, self.box_filter_window_name, -1)
        print 'Anker_X ', self.box_filter_anchor_x

    def on_anchor_y(self, value):
        # Werte [-1, 0, 1, 2, 3, ....]
        lis = range(-1, self.box_filter_anchor_y_max)
        val = lis[value]
        # Check ob Werte innerhalb der Kernelgroesse bleiben
        if -1 <= val < self.box_filter_kernel_y:
            self.box_filter_anchor_y = val
        else:
            raise ValueError("Wert Anker_Y == %s ist nicht erlaubt. Kernel-Hoehe == %s -> zu klein." %
                             (val, self.box_filter_kernel_y))
        if val == -1 and self.box_filter_anchor_x is not -1:    # anker x auch auf -1
            cv2.setTrackbarPos(self.box_filter_anchor_x_name, self.box_filter_window_name, -1)
        print 'Anker_Y ', self.box_filter_anchor_y

    def on_box_filter_kernel_x(self, value):
        l = [val for val in range(1, self.box_filter_kernel_x_max*2 + 4, 2)]  # nur ungerade Kernelgroessen
        val = l[value]
        if val <= self.box_filter_anchor_x:
            # setze default Anker-Werte (Mitte)
            print '\nSetze Anker auf Default-Werte zurueck (Mitte des Kernels).'
            cv2.setTrackbarPos(self.box_filter_anchor_x_name, self.box_filter_window_name, -1)
            cv2.setTrackbarPos(self.box_filter_anchor_y_name, self.box_filter_window_name, -1)
        self.box_filter_kernel_x = val
        print 'Kernel_X ', self.box_filter_kernel_x

    def on_box_filter_kernel_y(self, value):
        l = [val for val in range(1, self.box_filter_kernel_y_max*2 + 4, 2)]  # nur ungerade Kernelgroessen
        val = l[value]
        if val <= self.box_filter_anchor_y:
            # setze default Anker-Werte (Mitte)
            print '\nSetze Anker auf Default-Werte zurueck (Mitte des Kernels).'
            cv2.setTrackbarPos(self.box_filter_anchor_x_name, self.box_filter_window_name, -1)
            cv2.setTrackbarPos(self.box_filter_anchor_y_name, self.box_filter_window_name, -1)
        self.box_filter_kernel_y = val
        print 'Kernel_Y ', self.box_filter_kernel_y

    def on_ddepth(self, value):
        # Bildtiefen-Konstanten [0,6], cv2 api != cv api
        d_names = {-1: 'Ausgangsbild-Tiefe',
                   cv2.CV_8U: 'cv2.CV_8U',
                   cv2.CV_8S: 'cv2.CV_8S',  #  Fehler bei 3 Kanal Farbbild
                   cv2.CV_16U: 'cv2.CV_16U',
                   cv2.CV_16S: 'cv2.CV_16S',
                   cv2.CV_32S: 'cv2.CV_32S',
                   cv2.CV_32F: 'cv2.CV_32F',
                   cv2.CV_64F: 'cv2.CV_64F'
                   }
        depths = [-1,
                  cv2.CV_8U,
                  #cv2.CV_8S,  #  Fehler bei 3 Kanal Farbbild
                  cv2.CV_16U,
                  cv2.CV_16S,
                  cv2.CV_32S,
                  cv2.CV_32F,
                  cv2.CV_64F,
                  ]
        ddepth = depths[value]
        self.box_filter_ddepth = ddepth
        print 'Bildtiefe ', d_names[self.box_filter_ddepth]

    def read_image(self, file, method=cv2.CV_LOAD_IMAGE_COLOR):
        """Liest eine Bilddatei von Platte in gewuenschtem Farbmodus"""
        return cv2.imread(file, method)

if __name__ == '__main__':
    if len(sys.argv) != 2:
        print "--> usage: python u06a3.1.py <img>\nEsc -> Alle Fenster schließen"
        sys.exit(0)
    img = sys.argv[1]

    f = Filter(img)
    f.show()